#include "Syntax.h"
using namespace std;
extern vector<vector<SymbolTable>> synbl;       //各级符号表！！！！很重要！！！！
int curSynblLev = 0;						 //当前所在符号表的层次
int preSynblLev = -1;						 //前一层符号表的层次
int structTableIndex = 0;
int arrayTableIndex = 0;
extern vector<vector<RINFL>> rinfl;                         //记录结构体成员表
vector<AINFL> ainfl;                         //记录数组成员表
extern pair<int, int> ptr[SIZE_OF_RINFL];
Syntax::Syntax() {
    quat.open("quat.dat");
    Action_map["PUSH"] = _push;
    Action_map["POP"] = _pop;
    Action_map["LABER"] = _laber;
    Action_map["GOTO"] = _goto;
    Action_map["ASSI"] = _assi;
    Action_map["GEQ"] = _geq;
    Action_map["GEQ_UNARY"] = _geq_unary;
    Action_map["RELATION"] = _relation;
    Action_map["IF"] = _if;
    Action_map["ELSE"] = _else;
    Action_map["WHILE"] = _while;
    Action_map["DO"] = _do;
    Action_map["LOGIC"] = _logic;
    Action_map["SCOPEINIT"] = _scope_init;
    Action_map["PARAM"] = _param;
    Action_map["CALL"] = _call;
    Action_map["END"] = _end;
    Action_map["RETURN"] = _return;
    Action_map["FOR"] = _for;
    Action_map["FORINC"] = _for_inc;
    Action_map["FORSECPRE"] = _for_sec_pre;
    Action_map["FORSECPST"] = _for_sec_post;
    Action_map["FORTHDPRE"] = _for_third_pre;
    Action_map["FORTHDPST"] = _for_third_post;
    Action_map["DOWHILE"] = _do_while;
    Action_map["DOWHCMP"] = _do_while_cmp;
    Action_map["ENDDOWH"] = _end_dowh;
    Action_map["SCANF"] = _cin;
    Action_map["PRINT"] = _cout;
    Action_map["INITDECL"] = _init_decl;
    Action_map["INITARR"] = _init_arr;
    Action_map["GETFUNCNM"] = _get_func_name;
    Action_map["STRUCTINIT"] = _struct_scopeinit;
    Action_map["INITARRVAL"] = _init_arr_with_values;
    //类型表
    Tapel[0].Tval = TypeTable::_int;
    Tapel[0].Tpoint = -1;
    Tapel[1].Tval = TypeTable::_float;
    Tapel[1].Tpoint = -1;
    Tapel[2].Tval = TypeTable::_double;
    Tapel[2].Tpoint = -1;
    Tapel[3].Tval = TypeTable::_long;
    Tapel[3].Tpoint = -1;
    Tapel[4].Tval = TypeTable::_short;
    Tapel[4].Tpoint = -1;
    Tapel[5].Tval = TypeTable::_char;
    Tapel[5].Tpoint = -1;

    SymbolTable tmpsymb; //初始化全局符号表
    tmpsymb.prelev = -1;

    vector<SymbolTable> tmpvec;
    tmpvec.push_back(tmpsymb);
    synbl.push_back(tmpvec);
}

map<int, int> getSize = {
    {0, 2},
    {1, 2},
    {2, 4},
    {3, 4},
    {4, 1},
    {5, 2},
    {6, 0}, //void类型的变量不占内存
    {7, 0}, //数组类型的变量不占内存
    {8, 0}, //字符串类型的变量不占内存
    {9, 1}, //布尔类型的变量不占内存
    {10, 2}, //枚举类型的变量不占内存
    {-1, 0} //非法类型
};


std::map<std::string, std::pair<int, int>> Syntax::typeMap;

// 初始化类型映射表
void Syntax::initTypeMap() {
    typeMap = {
        {"int", {0, 2}},
        {"float", {1, 2}},
        {"double", {2, 4}},
        {"long", {3, 4}},
        {"short", {4, 1}},
        {"char", {5, 2}},
        {"struct", {6, 0}}, // 非法类型
        {"array", {7, 0}},  // 非法类型
        {"string", {8, 0}}, // 非法类型
        {"bool", {9, 1}},   // 非法类型
        {"enum", {10, 2}},  // 非法类型
        {"void", {-1, 0}}   // 非法类型
    };
}

TypeTable::VarType Syntax::getType(const string& ty) {
    if (ty == "int") return TypeTable::_int;
    else if (ty == "float") return TypeTable::_float;
    else if (ty == "double") return TypeTable::_double;
    else if (ty == "long") return TypeTable::_long;
    else if (ty == "short") return TypeTable::_short;
    else if (ty == "char") return TypeTable::_char;
    else if (ty == "void") return TypeTable::_void;
    else if (ty == "string") return TypeTable::_string; // 字符串类型
    else if (ty == "array") return TypeTable::_array;   // 数组类型
    else if (ty == "struct") return TypeTable::_struct; // 结构体类型
    else if (ty == "enum") return TypeTable::_enum;     // 枚举类型
    else if (ty == "union") return TypeTable::_union;   // 联合类型
    else if (ty == "pointer") return TypeTable::_pointer; // 指针类型
    else if (ty == "function") return TypeTable::_function; // 函数类型

}

// 获取类型码和大小
std::pair<int, int> Syntax::getTypeCode(const std::string& type) const {
    if (typeMap.empty()) {
        initTypeMap();
    }
    auto it = typeMap.find(type);
    if (it != typeMap.end()) {
        return it->second;
    }
    return { -1, 0 }; // 未定义类型
}

// 添加新类型
void Syntax::addTypeCode(const std::string& typeName, int typeCode, int size) {
    typeMap[typeName] = { typeCode, size };
}

bool Syntax::isExistInSynbl(string id, int index) {
    if (!synbl.empty() && index <= synbl.size()) {
        if (index >= synbl.size()) index = synbl.size() - 1;
        vector<SymbolTable>::iterator it = synbl[index].begin(), stor_it = it;
        for (; it != synbl[index].end(); it++) {
            if (it->name == id) {
                return true;
            }
        }
        if (it == synbl[index].end() || synbl[index][0].prelev != -1) {
            //当前作用域的符号表中未找到该标识符，且该作用域非全局作用域
            return isExistInSynbl(id, synbl[index][0].prelev);
        }
        else if (it == synbl[index].end() || synbl[index][0].prelev == -1) {
            return false;
        }
    }
    else return false;
}


int Syntax::getOffset(string id, int index) {
    if (!synbl.empty() && index <= synbl.size()) {
        if (index >= synbl.size()) index = synbl.size() - 1;
        vector<SymbolTable>::iterator it = synbl[index].begin(), stor_it = it;
        for (; it != synbl[index].end(); it++) {
            if (it->name == id) {
                return it->offset;
            }
        }
        if (it == synbl[index].end() || synbl[index][0].prelev != -1) {
            //当前作用域的符号表中未找到该标识符，且该作用域非全局作用域
            return getOffset(id, synbl[index][0].prelev);
        }
        else if (it == synbl[index].end() || synbl[index][0].prelev == -1) {
            return -1;
        }
    }
    else return -1;
}

int Syntax::getRealType(string id, int index) {
    if (!synbl.empty() && index <= synbl.size()) {
        if (index >= synbl.size()) index = synbl.size() - 1;
        vector<SymbolTable>::iterator it = synbl[index].begin(), stor_it = it;
        for (; it != synbl[index].end(); it++) {
            if (it->name == id) {
                return it->typ;
            }
        }
        if (it == synbl[index].end() || synbl[index][0].prelev != -1) {
            //当前作用域的符号表中未找到该标识符，且该作用域非全局作用域
            return getRealType(id, synbl[index][0].prelev);
        }
        else if (it == synbl[index].end() || synbl[index][0].prelev == -1) {
            return -1;
        }
    }
    else return -1;
}

bool isExistInCurDomain(string id, int index) {
    if (!synbl.empty() && index <= synbl.size()) {
        if (index >= synbl.size()) index = synbl.size() - 1;
        for (vector<SymbolTable>::iterator it = synbl[index].begin(); it != synbl[index].end(); it++) {
            if (it->name == id) {
                return true;
            }
        }
        return false;
    }
}

void printSEMStack(const stack<pair<string, int>>& SEM) {
    // stack<pair<string, int>> tmp = SEM; // 拷贝原栈，避免修改
    // cout << "【当前语义栈 SEM 内容】(从栈顶到底):" << endl;
    // while (!tmp.empty()) {
    //     cout << "内容: \"" << tmp.top().first << "\", Token索引: " << tmp.top().second << endl;
    //     tmp.pop();
    // }
}

int token_ptr = 0; //token索引
static int qtmpID = 0;
string tmpstr;
int paramId = 1; //标记第几个参数
pair<string, int> tmp, funcName;
extern int off; //暂存偏移量
bool isElif = false;

bool Syntax::Call(string Action, int TokenIndex, string curstr) {
    vector<SymbolTable> tmpSynbl; //临时单符号表
    SymbolTable tmpNode; //临时单符号表结点
    stack<pair<string, int>> tmpStack;
    AINFL tmpainfl; //临时数组成员表结点
    vector<RINFL> tmpRinfl;
    RINFL tmpr; //临时结构体成员信息
    //判定文件打开
    if (!quat) {
        cout << "四元式文件打开出错！" << endl;
        // exit(1);
        return false;
    }

    //cout << "*****************语义栈内容SEM***********************" << endl;
    switch (Action_map[Action])
    {
    case Action_id::_push: SEM.push(make_pair(curstr, TokenIndex));
        //for test
        //cout << curstr << "压入语义栈SEM" << endl;
        //for test
        break;
    case Action_id::_pop:
        while (!SEM.empty()) {
            if (tokenSeq[SEM.top().second].type == _constnum)
            {	//赋值语句 stack<pair<string, int>> SEM;
                //int a = 3, c = 9;
                string constnum = SEM.top().first; //记录下该常数
                SEM.pop(); //值出栈

                if (SEM.empty())continue;
                if (tokenSeq[SEM.top().second].type != _identifier)continue;

                //修改点：是否要上更高层级搜索
                //修改点：源代码强调要寻找是否发生重定义
                //int i;
                //for (i = 1; !synbl.empty() && curSynblLev < synbl.size() && i < synbl[curSynblLev].size(); i++) {
                //	//检查该变量是否已经定义过
                //	if (synbl[curSynblLev][i].name == SEM.top().first) {
                //		break;
                //	}
                //}
                if (!isExistInSynbl(SEM.top().first, curSynblLev)) {
                    cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                    cout << "error C2086: “int " << SEM.top().first << "”: 未定义." << endl;
                    // exit(1);
                    return false;
                }

                string name = SEM.top().first;

                //cout << "tmpNode.prelev = " << tmpNode.prelev << endl;//for test
                SEM.pop(); //id出栈
                stack<pair<string, int>> tmpSEM;
                while (!SEM.empty() && tokenSeq[SEM.top().second].type != _keyword) {
                    tmpSEM.push(SEM.top());
                    SEM.pop();
                } //暂且过滤掉同一行代码前面的定义性语句
                quat << "= " << constnum << " # " << name << endl; //生成四元式
                while (!tmpSEM.empty()) { //暂存的项倒回原语义栈中
                    SEM.push(tmpSEM.top());
                    tmpSEM.pop();
                }
            }
            SEM.pop();
        }
        break;
    case Action_id::_laber: quat << "label # # " << SEM.top().first << endl;
        SEM.pop();
        break;
    case Action_id::_goto: quat << "gt # # " << curstr << endl;
        break;
    case Action_id::_assi:
        if (!SEM.empty()) {
            tmp = SEM.top();
            SEM.pop(); //number is out!
            if (tmp.first == "return_val") {
                SEM.pop(); //pop =
                if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier) {
                    quat << "mov " << SEM.top().first << " # DX" << endl;
                }
            }
            else if (!SEM.empty() && SEM.top().first == "=") {
                SEM.pop(); //= is out
                if (!SEM.empty() && tokenSeq[SEM.top().second].type == _constnum) {
                    //数组赋值
                    pair<string, int> arroffset = SEM.top();
                    SEM.pop(); //a[index]的index出栈
                    //SEM.pop();
                    if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier && SEM.top().second != -1) {
                        SEM.pop(); //a[index]的a出栈
                        if (!SEM.empty() && tokenSeq[SEM.top().second].type == _keyword);
                        else
                            //quat << "[]= " << to_string(getOffset(SEM.top().first, curSynblLev) + stoi(arroffset.first) * 2) <<" "<< SEM.top().first <<" "<< tmp.first << endl;
                            quat << "[]= " << to_string(getOffset(SEM.top().first, curSynblLev) + stoi(arroffset.first) * 2) <<" "<< "#" << " " << tmp.first << endl;
                        SEM.push(tmp); //将赋值的值压入语义栈
                    }
                    else {
                        cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                        cout << "error C2065: “" << SEM.top().first << "”: 未声明的标识符" << endl;
                        // exit(1);
                        return false;
                    }
                }
                else if (!SEM.empty() && (tokenSeq[SEM.top().second].type == _identifier || SEM.top().second == -1)) {
                    if (SEM.top().second != -1 && !isExistInSynbl(SEM.top().first, curSynblLev)) {
                        //常规变量赋值
                        cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                        cout << "error C2065: “" << SEM.top().first << "”: 未声明的标识符" << endl;
                        // exit(1);
                        return false;

                    }
                    auto var = SEM.top(); //取得常规变量
                    if (!SEM.empty() && (tokenSeq[SEM.top().second].type == _identifier || SEM.top().second == -1)) {
                        SEM.pop(); //id is out
                        if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier && getRealType(SEM.top().first, curSynblLev) == TypeTable::_struct) {
                            quat << "= " << tmp.first << " # ";
                            quat << SEM.top().first << "." << var.first << endl;
                            SEM.pop();
                        }
                        else {
                            quat << "= " << tmp.first << " # ";
                            quat << var.first << endl;
                        }
                    }
                    SEM.push(tmp); //将赋值的值压入语义栈
                }
                //SEM.pop(); //delete it!
            }
            else if (!SEM.empty() && regex_match(SEM.top().first, regex("\\+=|-=|\\*=|/=|%=|&=|\\|=|^=|>>=|<<="))) {
                if (regex_match(SEM.top().first, regex("\\+=|-=|\\*=|/=|%=")))
                    quat << SEM.top().first[0] << " ";
                else if (SEM.top().first == "&=") quat << "and ";
                else if (SEM.top().first == "|=") quat << "or ";
                else if (SEM.top().first == "^=") quat << "not ";
                else if (SEM.top().first == ">>=") quat << "shr ";
                else if (SEM.top().first == "<<=") quat << "shl ";
                SEM.pop();
                if (SEM.top().second != -1 && !isExistInSynbl(SEM.top().first, curSynblLev)) {
                    cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                    cout << "error C2065: “" << SEM.top().first << "”: 未声明的标识符" << endl;
                    // exit(1);
                    return false;

                }
                quat << SEM.top().first << " " << tmp.first << " qtmp" + to_string(qtmpID) << endl;
                quat << "= qtmp" + to_string(qtmpID) << " # " << SEM.top().first << endl;
                //SEM.pop();//delete it!
                qtmpID++;
            }
            else if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier && tokenSeq[tmp.second].type == _constnum) {
                //数组元素赋值给一个变量,arr_index is out in tmp, int b = a .1
                int memPos = getOffset(SEM.top().first, curSynblLev) + stoi(tmp.first) * 2;
                pair<string, int> tmpa = SEM.top();
                SEM.pop(); //a is out
                if (!SEM.empty() && SEM.top().first == "=") {
                    // SEM.pop();//= is out
                    // quat << "=[] " << SEM.top().first << " # " << to_string(memPos) << endl;
                    SEM.pop();//= is out
                    quat << "=[] " << to_string(memPos) << " # " << SEM.top().first << endl;
                }
                else {
                    SEM.push(tmpa);
                    SEM.push(tmp);
                }
            }
            else if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier && tokenSeq[tmp.second].type == _identifier && getRealType(SEM.top().first, curSynblLev) == TypeTable::_struct) {
                auto struVar = SEM.top(); //取得结构体变量
                SEM.pop(); //结构体变量出栈
                SEM.pop(); //= is out
                if (!SEM.empty() && tokenSeq[SEM.top().second].type == _constnum) {
                    pair<string, int> arroffset = SEM.top();
                    SEM.pop(); //a[index]的index出栈
                    //SEM.pop();
                    if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier && SEM.top().second != -1) {
                        quat << "[]= " << to_string(getOffset(SEM.top().first, curSynblLev) + stoi(arroffset.first) * 2) << " " << SEM.top().first << " " << struVar.first<< "."<< tmp.first << endl;
                        SEM.pop(); //a[index]的a出栈
                        SEM.push(tmp); //将赋值的值压入语义栈
                    }
                    else {
                        cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                        cout << "error C2065: “" << SEM.top().first << "”: 未声明的标识符" << endl;
                        // exit(1);
                        return false;

                    }
                }
                else if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier) {
                    auto var = SEM.top(); //取得常规变量
                    SEM.pop(); //id is out
                    if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier && getRealType(SEM.top().first, curSynblLev) == TypeTable::_struct) {
                        quat << "= " << SEM.top().first << "." << var.first << " # ";
                        quat << struVar.first << "." << tmp.first << endl;
                        SEM.pop();
                    }
                    else {
                        quat << "= " << struVar.first << "." << tmp.first << " # ";
                        quat << var.first << endl;
                    }
                }
            }
            else SEM.push(tmp);
        }
        break;
    case Action_id::_init_arr_with_values:
        if (!SEM.empty() && tokenSeq[SEM.top().second].type == _constnum) {
            int i = 0;
            stack<pair<string, int>> tmpSEM;
            while (!SEM.empty() && tokenSeq[SEM.top().second].type != _keyword) {
                tmpSEM.push(SEM.top());
                SEM.pop();
            }
            while (!tmpSEM.empty() && !ainfl.empty()) {
                quat << "[]= " << ainfl.back().offset + i << " # " << tmpSEM.top().first << endl;
                i += getSize[ainfl.back().typ];
                tmpSEM.pop();
            }
        }
        break;
    case Action_id::_geq:
        tmpstr = SEM.top().first + " "; //cout << tmpstr << "is out!" << endl;//for test
        SEM.pop();
        quat << SEM.top().first << " ";   //cout << SEM.top().first << "is out!" << endl;//for test
        SEM.pop();
        quat << SEM.top().first << " " << tmpstr << "qtmp" + to_string(qtmpID) << endl;
        //cout << SEM.top().first << "is out!" << endl; //for test
        SEM.pop();
        SEM.push(make_pair("qtmp" + to_string(qtmpID), -1));
        qtmpID++;
        break;
    case Action_id::_geq_unary:
        if (!SEM.empty() && (SEM.top().first == "++" || SEM.top().first == "--")) {
            quat << SEM.top().first[0] << " ";
            SEM.pop();
            quat << SEM.top().first << " 1 " << "qtmp" + to_string(qtmpID) << endl;
            tmp = SEM.top();
            SEM.pop();
            quat << "= " << "qtmp" + to_string(qtmpID) << " # " << tmp.first << endl;
            SEM.push(tmp);
            qtmpID++;
        }
        else if (!SEM.empty() && (tokenSeq[SEM.top().second].type == _identifier || SEM.top().second == -1)) {
            tmp = SEM.top();
            SEM.pop();
            if (!SEM.empty() && SEM.top().first == "~") {
                quat << "not " << tmp.first << " # qtmp" + to_string(qtmpID) << endl;
                SEM.pop();
                SEM.push(make_pair("qtmp" + to_string(qtmpID), -1));
                qtmpID++;
            }
            else if (!SEM.empty() && (SEM.top().first == "++" || SEM.top().first == "--")) {
                quat << SEM.top().first[0] << " " << tmp.first << " 1 " << "qtmp" + to_string(qtmpID) << endl;
                quat << "= " << "qtmp" + to_string(qtmpID) << " # " << tmp.first << endl;
                SEM.pop();
                SEM.push(tmp);
                qtmpID++;
            }
            else SEM.push(tmp);
        }
        // else if (!SEM.empty() && (SEM.top().second == -1 || tokenSeq[SEM.top().second].type == _identifier || tokenSeq[SEM.top().second].type == _constnum)) {
        //     tmp = SEM.top();
        //     SEM.pop();
        //     if (!SEM.empty() && SEM.top().first == "~") {
        //         quat << "not " << tmp.first << " # qtmp" + to_string(qtmpID) << endl;
        //         SEM.pop();
        //         SEM.push(make_pair("qtmp" + to_string(qtmpID), -1));
        //         qtmpID++;
        //     }
        //     else SEM.push(tmp);
        // }
        break;
    case Action_id::_relation:
        if (SEM.top().second == -1 || tokenSeq[SEM.top().second].type == _identifier || tokenSeq[SEM.top().second].type == _constnum) {
            if (tokenSeq[SEM.top().second].type == _identifier) {
                //检查是否未定义
                if (SEM.top().second != -1 && !isExistInSynbl(SEM.top().first, curSynblLev)) {
                    cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                    cout << "error C2065: “" << SEM.top().first << "”: 未声明的标识符" << endl;
                    // exit(1);
                    return false;

                }
            }
            tmp = SEM.top();
            SEM.pop();
            if (SEM.top().first == "<" || SEM.top().first == ">" || SEM.top().first == "<=" || SEM.top().first == ">="
                || SEM.top().first == "==" || SEM.top().first == "!=") {
                quat << SEM.top().first << " ";
                SEM.pop();
                if (tokenSeq[SEM.top().second].type == _identifier) {
                    //检查是否未定义
                    if (SEM.top().second != -1 && !isExistInSynbl(SEM.top().first, curSynblLev)) {
                        //cout << "!isExistInSynbl(" << SEM.top().first << ", " << curSynblLev << ")" << endl;
                        cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                        cout << "error C2065: “" << SEM.top().first << "”: 未声明的标识符" << endl;
                        // exit(1);
                        return false;

                    }
                }
                quat << SEM.top().first << " " << tmp.first << " qtmp" + to_string(qtmpID) << endl;
                SEM.pop();
                SEM.push(make_pair("qtmp" + to_string(qtmpID), -1));
                qtmpID++;
            }
            else SEM.push(tmp);
        }
        break;
    case Action_id::_if:
        if (!SEM.empty() && SEM.top().second == -1) {
            if (isElif == false) { //只有if
                //cout << "------------IF------------" << endl;
                /*if (synbl.size() == 5)
                    cout << "stop";*/
                preSynblLev = curSynblLev;
                //cout << "preSynblLev = " << preSynblLev << endl;//for test
                curSynblLev = synbl.size(); //出了函数作用域后，要cur = pre;
                //cout << "curSynblLev = " << curSynblLev << endl;//for test
                //tmpNode.name = nullptr;
                tmpNode.prelev = preSynblLev;
                tmpNode.domainName = "if";
                tmpSynbl.push_back(tmpNode);
                synbl.push_back(tmpSynbl);
                //cout << "符号总表创建新节点" << synbl.size() - 1 << ": prelev = " << tmpNode.prelev << endl;//for test
                //cout << "------------IF------------" << endl;//for test

                quat << "if " << SEM.top().first << " # " << curSynblLev << endl;
                SEM.pop();
            }
            else {
                //else if...
                synbl[curSynblLev][0].domainName = "if";
                quat << "if " << SEM.top().first << " # " << curSynblLev << endl;
                SEM.pop();
            }
        }
        break;
    case Action_id::_else:
        preSynblLev = curSynblLev;
        curSynblLev = synbl.size(); //出了函数作用域后，要cur = pre;
        tmpNode.prelev = preSynblLev;
        tmpNode.domainName = "else";
        tmpSynbl.push_back(tmpNode);
        synbl.push_back(tmpSynbl);
        isElif = true;
        quat << "el # # " << curSynblLev << endl;
        break;
    case Action_id::_while:
        preSynblLev = curSynblLev;
        curSynblLev = synbl.size(); //出了函数作用域后，要cur = pre;
        tmpNode.prelev = preSynblLev;
        tmpNode.domainName = "while";
        tmpSynbl.push_back(tmpNode);
        synbl.push_back(tmpSynbl);

        quat << "wh # # " << curSynblLev << endl;
        break;
    case Action_id::_do:
        if (SEM.top().second == -1) {
            quat << "do " << SEM.top().first << " # #" << endl;
            SEM.pop();
        }
        break;
    case Action_id::_logic:
        if (!SEM.empty() && (SEM.top().second == -1 || tokenSeq[SEM.top().second].type == _identifier || tokenSeq[SEM.top().second].type == _constnum))
        {
            tmp = SEM.top();
            SEM.pop();
            if (!SEM.empty() && (SEM.top().first == "&&" || SEM.top().first == "&")) {
                SEM.pop(); //&& is out!
                quat << "and " << SEM.top().first << " " << tmp.first << " qtmp" + to_string(qtmpID) << endl;
                SEM.pop();
                SEM.push(make_pair("qtmp" + to_string(qtmpID), -1));
                qtmpID++;
            }
            else if (!SEM.empty() && (SEM.top().first == "||" || SEM.top().first == "|")) {
                SEM.pop(); //|| is out!
                quat << "or " << SEM.top().first << " " << tmp.first << " qtmp" + to_string(qtmpID) << endl;
                SEM.pop();
                SEM.push(make_pair("qtmp" + to_string(qtmpID), -1));
                qtmpID++;
            }
            else if (!SEM.empty() && (SEM.top().first == "^")) {
                SEM.pop(); //^ is out!
                quat << "xor " << SEM.top().first << " " << tmp.first << " qtmp" + to_string(qtmpID) << endl;
                SEM.pop();
                SEM.push(make_pair("qtmp" + to_string(qtmpID), -1));
                qtmpID++;
            }
            else if (!SEM.empty() && (SEM.top().first == "<<")) {
                SEM.pop();
                quat << "shl " << SEM.top().first << " " << tmp.first << " qtmp" + to_string(qtmpID) << endl;
                SEM.pop();
                SEM.push(make_pair("qtmp" + to_string(qtmpID), -1));
                qtmpID++;
            }
            else if (!SEM.empty() && (SEM.top().first == ">>")) {
                SEM.pop();
                quat << "shr " << SEM.top().first << " " << tmp.first << " qtmp" + to_string(qtmpID) << endl;
                SEM.pop();
                SEM.push(make_pair("qtmp" + to_string(qtmpID), -1));
                qtmpID++;
            }
            else SEM.push(tmp);
        }
        break;
    case Action_id::_scope_init:
        if (tokenSeq[SEM.top().second].type == _identifier) {
            preSynblLev = curSynblLev;   //当前层次变为上一层
            curSynblLev = synbl.size();  //当前层为符号总表中最新开辟的一层
            tmpNode.prelev = preSynblLev;//新作用域表的指针域记录上一层的索引号
            tmpNode.domainName = SEM.top().first;//记录新作用域的域名
            tmpSynbl.push_back(tmpNode);
            synbl.push_back(tmpSynbl);   //在符号总表中为新作用域开辟空间

            tmp = SEM.top();
            SEM.pop();
            if (tokenSeq[SEM.top().second].type == _keyword) {
                quat << "func " << tmp.first << " # " << preSynblLev << endl;
                //quat << "func "<<curSynblLev<<" # " << tmp.first << endl; //generate function quat
                SEM.pop(); //data type is out of stack!
                while (!SEM.empty()) { //开启一个新的作用域--函数，把SEM栈中之前的内容全部清空
                    SEM.pop();
                }
            }
            else SEM.push(tmp);
        }
        break;
    case Action_id::_get_func_name:
        if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier) {
            paramId = 1;
            funcName = SEM.top();
        }
        break;
    case Action_id::_param:
        //生成实参四元式
        if (SEM.top().second == -1 || tokenSeq[SEM.top().second].type == _identifier || tokenSeq[SEM.top().second].type == _constnum) {
            //quat << "param # # " << SEM.top().first << endl;
            int i;
            for (i = 0; i < synbl.size(); i++) {
                if (synbl[i][0].domainName == funcName.first) {
                    quat << "mov " << synbl[i][paramId].offset << " # " << SEM.top().first << endl;
                    break;
                }
            }
            if (i < synbl.size()) //找到了
                SEM.pop();
            else {
                //没找到该函数
                cout << "Line " << tokenSeq[funcName.second].row << ": ";
                cout << "'" << funcName.first << "' was not declared in this scope" << endl;
                // exit(1);
                return false;

            }
            paramId++;
        }
        break;
    case Action_id::_call:
        if (tokenSeq[SEM.top().second].type == _identifier) {
            quat << "call # # " << SEM.top().first << endl;
            SEM.pop();
            SEM.push(make_pair("return_val", -3));
        }
        break;
    case Action_id::_end:
        //cout << "---------END---------" << endl;//for test
        /*if (synbl[curSynblLev][0].domainName == "max")
            cout << "stop";*/
        quat << "end # # " << synbl[curSynblLev][0].domainName << endl;
        //cout<< "end # # " << synbl[curSynblLev][0].domainName << endl;//for test
        curSynblLev = preSynblLev;					//恢复当前作用域为上一层作用域
        preSynblLev = synbl[preSynblLev][0].prelev; //恢复上一层作用为上上层作用域
        //cout << "preSynblLev = " << preSynblLev << endl;//for test
        //cout << "---------END---------" << endl;//for test
        break;
    case Action_id::_return:
        if (!SEM.empty() && (SEM.top().second == -1 || tokenSeq[SEM.top().second].type == _identifier || tokenSeq[SEM.top().second].type == _constnum)) {
            quat << "re " << SEM.top().first << " # #" << endl;
            SEM.pop();
        }
        else if (SEM.empty()) {
            quat << "re void # #" << endl;
        }
        break;
    case Action_id::_for:
        preSynblLev = curSynblLev;
        curSynblLev = synbl.size();
        tmpNode.prelev = preSynblLev;
        tmpNode.domainName = "for";
        tmpSynbl.push_back(tmpNode);
        synbl.push_back(tmpSynbl);
        quat << "for # # " << curSynblLev << endl;
        break;
    case Action_id::_for_inc:
        if (synbl[curSynblLev][0].domainName == "for") {
            quat << "jmp # # inc" << endl;
        }
        break;
    case Action_id::_for_sec_pre:
        if (synbl[curSynblLev][0].domainName == "for") {
            quat << "label # # cmp" << endl;
        }
        break;
    case Action_id::_for_sec_post:
        if (synbl[curSynblLev][0].domainName == "for") {
            if (SEM.top().second == -1) {
                quat << "do " << SEM.top().first << " # #" << endl;
                quat << "jmp # # end" << endl;
                quat << "jmp # # start" << endl;
            }
        }
        break;
    case Action_id::_for_third_pre:
        if (synbl[curSynblLev][0].domainName == "for") {
            quat << "label # # inc" << endl;
        }
        break;
    case Action_id::_for_third_post:
        if (synbl[curSynblLev][0].domainName == "for") {
            quat << "jmp # # cmp" << endl;
            quat << "label # # start" << endl;
        }
        break;
    case Action_id::_do_while:
        preSynblLev = curSynblLev;
        curSynblLev = synbl.size(); //出了函数作用域后，要cur = pre;
        tmpNode.prelev = preSynblLev;
        tmpNode.domainName = "dowhile";
        tmpSynbl.push_back(tmpNode);
        synbl.push_back(tmpSynbl);

        quat << "dowhile # # " << curSynblLev << endl;
        quat << "label # # start" << endl;
        break;
    case Action_id::_do_while_cmp:
        if (synbl[curSynblLev][0].domainName == "dowhile") {
            quat << "dowhcmp # # #" << endl;
        }
        break;
    case Action_id::_end_dowh:
        if (SEM.top().second == -1) {
            quat << "do " << SEM.top().first << " # #" << endl;
            quat << "jmp # # start" << endl;
            //quat << "end # # dowhile" << endl;
        }
        break;
    case Action_id::_cin:
        if (tokenSeq[SEM.top().second].type == _identifier) {
            quat << "cin # # " << SEM.top().first << endl;
        }
        break;
    case Action_id::_cout:
        if (!SEM.empty() && (tokenSeq[SEM.top().second].type == _identifier || tokenSeq[SEM.top().second].type == _constnum)) {
            tmp = SEM.top();
            if (tokenSeq[SEM.top().second].type == _identifier) {
                //输出的是变量的值
                if (!isExistInSynbl(SEM.top().first, curSynblLev)) {
                    cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                    cout << "error C2065: “" << SEM.top().first << "”: 未声明的标识符" << endl;
                    // exit(1);
                    return false;

                }
            }
            SEM.pop(); //输出内容出栈
            if (!SEM.empty() && tokenSeq[SEM.top().second].type == _constnum)
            {
                quat << "cout " << SEM.top().first << " # " << tmp.first << endl;
                SEM.pop();
            }
            //else SEM.push(tmp);
            else quat << "cout 10 # " << tmp.first << endl;
        }
        break;
    case Action_id::_init_decl:
    {
        //临时变量
        pair<string, int> tt;
        vector<vector<RINFL>>::iterator it;
        if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier) {
            //检测变量是否已经被定义
            int i;
            for (i = 1; !synbl.empty() && curSynblLev < synbl.size() && i < synbl[curSynblLev].size(); i++) {
                //重复定义
                if (synbl[curSynblLev][i].name == SEM.top().first) {
                    cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                    cout << "error C2086: “ " << SEM.top().first << "”: 重定义." << endl;
                    // exit(1);
                    return false;

                }
            }
            //取出当前变量
            tmp = SEM.top();
            SEM.pop(); //id is out

            //注意点：对于struct Node t;这种情况 tmpNode会存入Node

            stack<pair<string, int>> tmpSEM;
            while (!SEM.empty() && (tokenSeq[SEM.top().second].type != _keyword)) {
                tmpSEM.push(SEM.top());
                SEM.pop();
            }

            //设置变量的前一作用域（因为是定义语句，作用域为cur，保持不变）
            tmpNode.prelev = preSynblLev;

            //设置符号表名字属性为当前变量名
            tmpNode.name = tmp.first;

            //tmpSEM栈保存前面没处理的变量
            auto tmpType = SEM.top();
            SEM.pop(); //type is out

            //查找变量类型，填写符号表的TYP类型区
            //修改点：对于struct Node t;这种情况，返回{6, 0}大小没初始化
            auto getTy = getTypeCode(tmpType.first);

            tmpNode.typ = getTy.first;
            if (tmpNode.typ == -1) {
                //void类型被用于定义变量，错误处理
                cout << "Line " << tokenSeq[tmp.second].row << ": ";
                cout << "error C2182: “" << tmpNode.name << "”: 非法使用“void”类型." << endl;
                // exit(1);
                return false;

            }	//struct Node a,b,c;
            else if (!synbl[curSynblLev].empty() && tmpType.first == "struct") {
                stack<pair<string, int>> defineSEM;
                while (!tmpSEM.empty()) {
                    int j = 0;
                    for (; j < structTableIndex; j++) {
                        int x = ptr[j].first, y = ptr[j].second;
                        if (rinfl[x][y].structName == tmpSEM.top().first) {
                            goto label;
                        }
                    }
                    defineSEM.push(tmpSEM.top());
                    tmpSEM.pop();
                }
            label:
                if (!tmpSEM.empty()) {
                    int i = 0;
                    for (; i < structTableIndex; i++) {
                        int x = ptr[i].first, y = ptr[i].second;
                        if (rinfl[x][y].structName == tmpSEM.top().first) {
                            break;
                        }
                    }
                    if (i < structTableIndex) {
                        vector<vector<RINFL>>::iterator it = rinfl.begin();
                        for (; it != rinfl.end(); it++) {
                            if (it->front().structName == tmpSEM.top().first) {
                                break;
                            }
                        }
                        if (it != rinfl.end()) {
                            tmpNode.name = tmp.first;
                            tmpNode.cat = SymbolTable::_variable;
                            tmpNode.prelev = preSynblLev;
                            tmpNode.offset = off; //设置偏移量
                            tmpNode.typ = getType(tmpType.first); //设置类型
                            tmpNode.domainName = it->front().structName; //记录结构体名


                            tmpr.name = tmp.first; //设置结构体名
                            tmpr.offset = off; //设置结构体偏移量
                            off += it->front().size; //更新偏移量
                            tmpr.structName = it->front().structName; //设置结构体名
                            tmpr.size = it->front().size; //设置结构体大小
                            for (vector<Domain>::iterator j = it->front().memberName.begin(); j != it->front().memberName.end(); j++) {
                                tmpr.memberName.push_back(*j); //记录结构体成员名
                            }
                            int start = 0;
                            for (vector< Domain>::iterator j = tmpr.memberName.begin(); j != tmpr.memberName.end(); j++) {
                                j->offset = start;//设置结构体成员偏移量
                                start += getSize[j->typ];
                            }
                            it->push_back(tmpr); //将结构体成员信息加入结构体信息表
                        }
                    }
                    while (!defineSEM.empty()) {
                        //将之前的定义项倒回原语义栈中
                        SEM.push(tmpSEM.top());
                        defineSEM.pop();
                    }

                }
                else {
                    //void类型被用于定义变量，错误处理
                    cout << "Line " << tokenSeq[tmp.second].row << ": ";
                    cout << "error C????: “" << tmpNode.name << "”: tmpSEM为空哟." << endl;
                    // exit(1);
                    return false;

                }
            }	//定义结构体内部属性
            else if (!synbl[curSynblLev].empty() && (synbl[curSynblLev].back().typ == TypeTable::_struct || synbl[curSynblLev].back().cat == SymbolTable::Category::_domain)&& !SEM.empty()) {
                stack<pair<string, int>> defineSEM;
                while (!SEM.empty() && getRealType(SEM.top().first,curSynblLev) != TypeTable::_struct) {
                    if (getRealType(SEM.top().first, curSynblLev) == TypeTable::_struct) {
                        int j = 0;
                        for (; j < structTableIndex; j++) {
                            int x = ptr[j].first, y = ptr[j].second;
                            if (rinfl[x][y].structName == SEM.top().first) {
                                goto label1;
                            }
                        }
                    }
                    defineSEM.push(SEM.top());
                    SEM.pop();
                }
            label1:
                //if(rinfl[ptr[structTableIndex - 1].first][ptr[structTableIndex - 1].second].structName == SEM.top().first)
                tmpNode.name = tmp.first; //设置结构体名
                tmpNode.typ = getType(tmpType.first); //设置类型
                tmpNode.cat = SymbolTable::_domain;
                Domain tmpd;
                tmpd.id = tmp.first;
                int x = ptr[structTableIndex - 1].first, y = ptr[structTableIndex - 1].second;
                tmpd.offset = rinfl[x][y].offset;
                tmpd.typ = tmpNode.typ; //设置类型
                rinfl[x][y].memberName.push_back(tmpd);

                tmpNode.offset = -1;
                rinfl[x][y].size += getTypeCode(tmpType.first).second; //更新结构体大小
                while (!defineSEM.empty()) {
                    //将之前的定义项倒回原语义栈中
                    SEM.push(defineSEM.top());
                    defineSEM.pop();
                }
            }
            else {
                tmpNode.offset = off; //设置偏移量
                tmpNode.cat = SymbolTable::_variable; //设置类别为变量
                off += getTy.second;
                cout << "定义变量" << tmpNode.name << "，偏移量为" << tmpNode.offset << endl; //for test
            }
            //单符号表已经存在的情况
            if (!synbl.empty() && curSynblLev < synbl.size()) {
                synbl[curSynblLev].push_back(tmpNode);
                //	cout << endl << tmpNode.name << "进入synbl[" << curSynblLev << "]" << endl; //for test
            }
            else {
                //当前层次是首次填写，即单符号表不存在的情况
                tmpSynbl.push_back(tmpNode); //当前结点加入单符号表中
                synbl.push_back(tmpSynbl);
                //	cout << "创建总表新节点" << synbl.size() - 1 << ": " << tmpNode.name << "进入";
                //	cout << ", prevel = " << tmpNode.prelev << endl;//for test
            }
            SEM.push(tmpType);
            while (!tmpSEM.empty()) {
                //将之前的定义项倒回原语义栈中
                SEM.push(tmpSEM.top());
                tmpSEM.pop();
            }
            SEM.push(tmp);
        }
    }
    break;
    case Action_id::_init_arr:
        if (!SEM.empty() && tokenSeq[SEM.top().second].type == _constnum) {
            tmp = SEM.top();
            SEM.pop();
            if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier) {
                //vector<SymbolTable> tmpSynbl; //临时单符号表
                //SymbolTable tmpNode; //临时单符号表结点
                tmpNode.name = SEM.top().first;
                pair<string, int> idtmp = SEM.top();
                SEM.pop();//id is out!
                tmpNode.prelev = preSynblLev;
                tmpNode.offset = off;
                tmpNode.cat = SymbolTable::_variable; //设置类别为变量
                if (!SEM.empty() && tokenSeq[SEM.top().second].type == _keyword) {
                    tmpNode.typ = getTypeCode(SEM.top().first).first;
                    if (tmpNode.typ == -1) {
                        //void类型被用于定义变量，错误处理
                        cout << "Line " << tokenSeq[tmp.second].row << ": ";
                        cout << "error C2182: “" << tmpNode.name << "”: 非法使用“void”类型." << endl;
                        // exit(1);
                        return false;

                    }
                    off += getTypeCode(SEM.top().first).second * (stoi(tmp.first));
                    synbl[curSynblLev].push_back(tmpNode);
                    //SEM.pop();//int is out
                    SEM.push(idtmp);
                    SEM.push(tmp);
                }
                tmpainfl.len = stoi(tmp.first); //数组长度
                tmpainfl.typ = tmpNode.typ; //数组类型
                tmpainfl.low = 0;
                tmpainfl.up = tmpainfl.len - 1; //数组下标从0开始
                tmpainfl.name = tmpNode.name; //数组名
                tmpainfl.offset = tmpNode.offset; //数组偏移量
                ainfl.push_back(tmpainfl); //将数组信息加入数组信息表
                arrayTableIndex++;
                tmpNode.typ = TypeTable::VarType::_array; //标记为数组类型
                tmpNode.domainName = idtmp.first; //记录数组名
            }
        }
        break;
    case Action_id::_struct_scopeinit:
        if (!SEM.empty() && tokenSeq[SEM.top().second].type == _identifier) {
            int i;
            for (i = 1; !synbl.empty() && curSynblLev < synbl.size() && i < synbl[curSynblLev].size(); i++) {
                //重复定义
                if (synbl[curSynblLev][i].name == SEM.top().first) {
                    cout << "Line " << tokenSeq[SEM.top().second].row << ": ";
                    cout << "error C2086: “ " << SEM.top().first << "”: 重定义." << endl;
                    // exit(1);
                    return false;

                }
            }
            // preSynblLev = curSynblLev;
            // curSynblLev = 0; //开辟一个新的域，结构体作用域
            tmpNode.prelev = preSynblLev;
            tmpNode.cat = SymbolTable::_type;
            tmpNode.typ = TypeTable::_struct; //标记为结构体类型
            tmp = SEM.top();
            tmpNode.domainName = "struct";
            tmpNode.name = tmp.first; //结构体名
            tmpNode.offset = -1; //结构体偏移量从0开始
            SEM.pop(); //id is out!
            if (!SEM.empty() && SEM.top().first == "struct") {
                quat << "struct " << tmp.first << " # " << tmpNode.prelev << endl;
                auto sto = SEM.top();
                SEM.pop(); //'struct' is out!
                if (!synbl.empty() && curSynblLev < synbl.size()) {
                    synbl[curSynblLev].push_back(tmpNode);
                    //	cout << endl << tmpNode.name << "进入synbl[" << curSynblLev << "]" << endl; //for test
                }
                else {
                    //当前层次是首次填写，即单符号表不存在的情况
                    tmpSynbl.push_back(tmpNode); //当前结点加入单符号表中
                    synbl.push_back(tmpSynbl);

                }

                tmpr.structName = tmp.first; //记录结构体名
                tmpr.name = "";
                tmpr.size = 0; //结构体初始大小为0
                tmpr.offset = 0; //结构体初始偏移量为0
                tmpRinfl.push_back(tmpr);
                rinfl.push_back(tmpRinfl); //为结构体开辟一个新的成员信息表
                int i = 0;
                for (; i < structTableIndex; i++) {
                    int x = ptr[i].first, y = ptr[i].second;
                    if (rinfl[x][y].structName == tmp.first)break;
                }
                if(!rinfl.empty() && i <= structTableIndex && !rinfl.back().empty())ptr[structTableIndex++] = { rinfl.size() - 1, 0 };
                SEM.push(sto);
                SEM.push(tmp);

            }
            else SEM.push(tmp);
        }
        break;
    }
    // printSEMStack(SEM);

    //for test
    /*stack<pair<string, int>> tmpStack2;
    while (!SEM.empty()) {
        tmpStack2.push(SEM.top());
        SEM.pop();
    }
    while (!tmpStack2.empty()) {
        cout << tmpStack2.top().first << ", ";
        SEM.push(tmpStack2.top());
        tmpStack2.pop();
    }cout << endl;*/
    //showSynbl();
    ////for test
    //cout << "****************END********************" << endl;
    return true;
}

void Syntax::showSynbl() {
    ofstream outSynbl;
    outSynbl.open("synbl.csv", ios::out | ios::trunc); //清空文件
    for (int i = 0; i < synbl.size(); i++)
    {
        if (i == 0) {
            outSynbl << "Scope " + to_string(i) << ": Global Scope" << endl;
            //cout << "Scope " + to_string(i) << ": Global Scope" << endl;
        }
        else {
            outSynbl << "Scope " + to_string(i) << ": " << synbl[i][0].domainName << endl;
            //cout << "Scope " + to_string(i) << ": " << synbl[i][0].domainName << endl;
        }
        if (synbl[i].size() > 1)outSynbl << "Name,DataType,Offset,Pre-domain,Cat" << endl;
        for (int j = 1; j < synbl[i].size(); j++) {
            outSynbl << synbl[i][j].name << ",";
            if (synbl[i][j].typ == 0)outSynbl << "int,";
            else if (synbl[i][j].typ == 1)outSynbl << "float,";
            else if (synbl[i][j].typ == 2)outSynbl << "double,";
            else if (synbl[i][j].typ == 3)outSynbl << "long,";
            else if (synbl[i][j].typ == 4)outSynbl << "short,";
            else if (synbl[i][j].typ == 5)outSynbl << "char,";
            else if (synbl[i][j].typ == 6) outSynbl << "struct,";
            else if (synbl[i][j].typ == -1)outSynbl << "void,";
            outSynbl << synbl[i][j].offset << ",";
            outSynbl << synbl[i][j].prelev << ",";
            if (synbl[i][j].cat == SymbolTable::Category::_variable) outSynbl << "variable,";
            else if (synbl[i][j].cat == SymbolTable::Category::_type) outSynbl << "type,";
            else if (synbl[i][j].cat == SymbolTable::Category::_domain) outSynbl << "domain,";
            else if (synbl[i][j].cat == SymbolTable::Category::_function) outSynbl << "function,";
            else if (synbl[i][j].cat == SymbolTable::Category::_constant) outSynbl << "constant,";
            else if (synbl[i][j].cat == SymbolTable::Category::_vn) outSynbl << "vn,";
            else if(synbl[i][j].cat == SymbolTable::Category::_vf) outSynbl << "vf,";
            else outSynbl << "unknown,";
            outSynbl << endl;
        }
    }
    //输出rinfl
    for (vector<vector<RINFL>>::iterator it = rinfl.begin(); it != rinfl.end(); it++) {
        outSynbl << "define type:" << endl << to_string(it->front().offset) << to_string(it->front().size) << it->front().structName << endl;
        for (vector<Domain>::iterator jt = it->front().memberName.begin(); jt != it->front().memberName.end(); jt++) {
            outSynbl << jt->id << " " << to_string(jt->offset) << " " << to_string(jt->typ) << endl;
        }
        for (vector<RINFL>::iterator jt = it->begin() + 1; jt != it->end(); jt++) {
            outSynbl << jt->name << to_string(jt->offset) << to_string(jt->size) << jt->structName << endl;
            for (vector<Domain>::iterator kt = jt->memberName.begin(); kt != jt->memberName.end(); kt++) {
                outSynbl << kt->id << " " << to_string(kt->offset) << " " << to_string(kt->typ) << endl;
            }
        }
    }
    outSynbl << "rinflNam  " << "structName "<< "offset "<<"size  "<<" memberName " << endl;
    for (vector<AINFL>::iterator it = ainfl.begin(); it != ainfl.end(); it++) {
        outSynbl << it->name << " " << it->typ << " " << it->len << " " << it->low << " " << it->up << " " << it->offset << endl;
    }
    //for (int i= 0; i < structTableIndex; i++) {
    //	/*outSynbl << "Struct " << rinfl[ptr[i].first][ptr[i].second].structName << ": ";
    //	for (int j = 0; j < rinfl[ptr[i].first][ptr[i].second].memberName.size(); j++) {
    //		outSynbl << rinfl[ptr[i].first][ptr[i].second].memberName[j].id << ",";
    //	}
    //	outSynbl << endl;*/
    //	auto it = ptr[i];
    //	for()

    //}
}



//POP cut
//else if (tokenSeq[SEM.top().second].type == _identifier) {
//	//定义性语句 int b;
//	SEM.pop(); //id出栈
//	stack<pair<string, int>> tmpSEM;
//	while (!SEM.empty() && tokenSeq[SEM.top().second].type != _keyword) {
//		tmpSEM.push(SEM.top());
//		SEM.pop();
//	}

//	while (!tmpSEM.empty()) { //暂存的项倒回原语义栈中
//		SEM.push(tmpSEM.top());
//		tmpSEM.pop();
//	}
//}

//if (tmp.first == "b") {
//	cout << "1";
//}
//for (i = 1; !synbl.empty() && curSynblLev < synbl.size() && i < synbl[curSynblLev].size(); i++) {
//	//重复定义
//	if (synbl[curSynblLev][i].name == SEM.top().first && synbl[curSynblLev][i].cat == SymbolTable::Category::_type) {
//		break;
//	} && synbl[curSynblLev][i].typ != TypeTable::_struct
//
// 			if (synbl[curSynblLev][i].typ == TypeTable::_struct) {
//	tt = SEM.top();
//	SEM.pop();
// }
//}

//结构体定义变量
//for (it = rinfl.begin(); it != rinfl.end(); it++) {
//	if (it->begin()->structName == tmpSEM.top().first) {
//		break;
//	}
//}
//if (it != rinfl.end()) {
//	tmpNode.cat = SymbolTable::_domain; //标记为结构体类型
//	tmpNode.offset = 0; //结构体偏移量从0开始
//	tmpNode.prelev = -1; //结构体的prelev为-1
//	tmpNode.domainName = it->front().structName; //记录结构体名
//	tmpNode.typ = getType(tmpType.first); //设置类型为结构体类型
//	it->front().size += getTypeCode(tmpType.first).second;
//	it->front().memberName.push_back(tmpNode.name);
//	//it->push_back(tmpr); //将结构体成员信息加入结构体信息表
//}

/*


//记录之前的偏移量
                tmpNode.offset = off;
                //查找变量大小，更新当前偏移量

                cout << "$$$$$$$$$$$$$$$$$$$$$$$" << tmpType.first << "$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
                if (getTy.first == 9) {
                    if (!tmpSEM.empty()) {
                        tt = tmpSEM.top();
                        tmpSEM.pop();
                    }
                    auto rit = rinfl.begin();
                    for (; rit != rinfl.end(); rit++) {
                        if (rit->front().structName == tt.first) {
                            break;
                        }
                    }

                    tmpNode.typ = TypeTable::_struct; //标记为结构体类型
                    tmpNode.cat = SymbolTable::_domain; //标记为结构体类型
                    tmpNode.domainName = tt.first; //记录结构体名
                    tmpNode.offset = off; //结构体偏移量
                    RINFL tmpr;
                    off += rit->front().size; //结构体大小
                    for (vector<string>::iterator i = rit->front().memberName.begin(); i != rit->front().memberName.end(); i++) {
                        tmpr.memberName.push_back(*i); //记录结构体成员名
                    }
                    tmpr.name = tmp.first;
                    tmpr.offset = off;
                    tmpr.size = rit->front().size; //结构体成员大小
                    tmpr.structName = rit->front().structName; //结构体名
                    rit->push_back(tmpr);
                    off += rit->front().size; //更新偏移量
                }
                else {
                     //更新偏移量

                    //SEM.push(tmpType);
                    //SEM.push(tmp);
                }
                //off += getTy.second; //更新偏移量
                //for test
                //cout << "$$$$$$$$$$$$$$$$$$$$$$$$$ off = " << off << endl;




*/
